



<!doctype html>
<html lang="en" class="no-js">
  <head>

<style>
  h1, h2, h3 { color: #04A9F4; }
  pre { color: black !important; }
</style>


      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">

        <meta name="description" content="Open source package for Survival Analysis modeling">


        <link rel="canonical" href="https://www.pysurvival.io/tutorials/credit_risk.html">


        <meta name="author" content="The PySurvival Team">


        <meta name="lang:clipboard.copy" content="Copy to clipboard">

        <meta name="lang:clipboard.copied" content="Copied to clipboard">

        <meta name="lang:search.language" content="en">

        <meta name="lang:search.pipeline.stopwords" content="True">

        <meta name="lang:search.pipeline.trimmer" content="True">

        <meta name="lang:search.result.none" content="No matching documents">

        <meta name="lang:search.result.one" content="1 matching document">

        <meta name="lang:search.result.other" content="# matching documents">

        <meta name="lang:search.tokenizer" content="[\s\-]+">

      <link rel="shortcut icon" href="../images/favicon.ico">
      <meta name="generator" content="mkdocs-1.0.4, mkdocs-material-4.0.2">



        <title>Credit Risk - PySurvival</title>



      <link rel="stylesheet" href="../assets/stylesheets/application.982221ab.css">

        <link rel="stylesheet" href="../assets/stylesheets/application-palette.224b79ff.css">




        <meta name="theme-color" content="#2196f3">



      <script src="../assets/javascripts/modernizr.1f0bcf2b.js"></script>



        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=:300,400,400i,700|">
        <style>body,input{font-family:"","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"","Courier New",Courier,monospace}</style>


    <link rel="stylesheet" href="../assets/fonts/material-icons.css">






  </head>



    <body dir="ltr" data-md-color-primary="blue" data-md-color-accent="purple">

    <svg class="md-svg">
      <defs>


          <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448"
    viewBox="0 0 416 448" id="__github">
  <path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19-18.125
        8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19 18.125-8.5
        18.125 8.5 10.75 19 3.125 20.5zM320 304q0 10-3.125 20.5t-10.75
        19-18.125 8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19
        18.125-8.5 18.125 8.5 10.75 19 3.125 20.5zM360
        304q0-30-17.25-51t-46.75-21q-10.25 0-48.75 5.25-17.75 2.75-39.25
        2.75t-39.25-2.75q-38-5.25-48.75-5.25-29.5 0-46.75 21t-17.25 51q0 22 8
        38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0
        37.25-1.75t35-7.375 30.5-15 20.25-25.75 8-38.375zM416 260q0 51.75-15.25
        82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5-41.75
        1.125q-19.5 0-35.5-0.75t-36.875-3.125-38.125-7.5-34.25-12.875-30.25-20.25-21.5-28.75q-15.5-30.75-15.5-82.75
        0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25
        30.875q36.75-8.75 77.25-8.75 37 0 70 8 26.25-20.5
        46.75-30.25t47.25-9.75q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34
        99.5z" />
</svg>

      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>

      <a href="#computing-the-speed-of-repayment-of-loans" tabindex="1" class="md-skip">
        Skip to content
      </a>


      <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="https://www.pysurvival.io/" title="PySurvival" class="md-header-nav__button md-logo">

            <img src="../images/logo.ico" width="24" height="24">

        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">

            <span class="md-header-nav__topic">
              PySurvival
            </span>
            <span class="md-header-nav__topic">
              Credit Risk
            </span>

        </div>
      </div>

              <!-- Global site tag (gtag.js) - Google Analytics -->
              <script async src="https://www.googletagmanager.com/gtag/js?id=UA-136790579-1"></script>
              <script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());

                gtag('config', 'UA-136790579-1');
              </script>

      <div class="md-flex__cell md-flex__cell--shrink">

          <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>

<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>

      </div>

        <div class="md-flex__cell md-flex__cell--shrink">
          <div class="md-header-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
          </div>
        </div>

    </div>
  </nav>
</header>

    <div class="md-container">




      <main class="md-main">
        <div class="md-main__inner md-grid" data-md-component="container">


              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="https://www.pysurvival.io/" title="PySurvival" class="md-nav__button md-logo">

        <img src="../images/logo.ico" width="48" height="48">

    </a>
    PySurvival
  </label>

    <div class="md-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
    </div>

  <ul class="md-nav__list" data-md-scrollfix>






  <li class="md-nav__item">
    <a href="../index.html" title="Home" class="md-nav__link">
      Home
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../installation.html" title="Installation" class="md-nav__link">
      Installation
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../intro.html" title="Introduction to Survival Analysis" class="md-nav__link">
      Introduction to Survival Analysis
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../math.html" title="The math of Survival Analysis" class="md-nav__link">
      The math of Survival Analysis
    </a>
  </li>









  <li class="md-nav__item md-nav__item--active md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-5" type="checkbox" id="nav-5" checked>

    <label class="md-nav__link" for="nav-5">
      Tutorials
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-5">
        Tutorials
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="churn.html" title="Churn Prediction" class="md-nav__link">
      Churn Prediction
    </a>
  </li>









  <li class="md-nav__item md-nav__item--active">

    <input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">




      <label class="md-nav__link md-nav__link--active" for="__toc">
        Credit Risk
      </label>

    <a href="credit_risk.html" title="Credit Risk" class="md-nav__link md-nav__link--active">
      Credit Risk
    </a>


<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#1-introduction" title="1 - Introduction" class="md-nav__link">
    1 - Introduction
  </a>

</li>

        <li class="md-nav__item">
  <a href="#2-set-up" title="2 - Set up" class="md-nav__link">
    2 - Set up
  </a>

</li>

        <li class="md-nav__item">
  <a href="#3-dataset" title="3 - Dataset" class="md-nav__link">
    3 - Dataset
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#31-overview" title="3.1 - Overview" class="md-nav__link">
    3.1 - Overview
  </a>

</li>

          <li class="md-nav__item">
  <a href="#32-from-categorical-to-numerical" title="3.2 - From categorical to numerical" class="md-nav__link">
    3.2 - From categorical to numerical
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#4-exploratory-data-analysis" title="4 - Exploratory Data Analysis" class="md-nav__link">
    4 - Exploratory Data Analysis
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#41-null-values-and-duplicates" title="4.1 - Null values and duplicates" class="md-nav__link">
    4.1 - Null values and duplicates
  </a>

</li>

          <li class="md-nav__item">
  <a href="#42-correlations" title="4.2 - Correlations" class="md-nav__link">
    4.2 - Correlations
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#5-modeling" title="5 - Modeling" class="md-nav__link">
    5 - Modeling
  </a>

</li>

        <li class="md-nav__item">
  <a href="#6-cross-validation" title="6 - Cross Validation" class="md-nav__link">
    6 - Cross Validation
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#61-c-index" title="6.1 - C-index" class="md-nav__link">
    6.1 - C-index
  </a>

</li>

          <li class="md-nav__item">
  <a href="#62-brier-score" title="6.2 - Brier Score" class="md-nav__link">
    6.2 - Brier Score
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#7-predictions" title="7 - Predictions" class="md-nav__link">
    7 - Predictions
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#71-overall-predictions" title="7.1 - Overall predictions" class="md-nav__link">
    7.1 - Overall predictions
  </a>

</li>

          <li class="md-nav__item">
  <a href="#72-individual-predictions" title="7.2 - Individual predictions" class="md-nav__link">
    7.2 - Individual predictions
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#8-conclusion" title="8 - Conclusion" class="md-nav__link">
    8 - Conclusion
  </a>

</li>

        <li class="md-nav__item">
  <a href="#references" title="References" class="md-nav__link">
    References
  </a>

</li>





    </ul>

</nav>

  </li>







  <li class="md-nav__item">
    <a href="employee_retention.html" title="Employee Retention" class="md-nav__link">
      Employee Retention
    </a>
  </li>







  <li class="md-nav__item">
    <a href="maintenance.html" title="Predictive Maintenance" class="md-nav__link">
      Predictive Maintenance
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6" type="checkbox" id="nav-6">

    <label class="md-nav__link" for="nav-6">
      Models
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-6">
        Models
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-1" type="checkbox" id="nav-6-1">

    <label class="md-nav__link" for="nav-6-1">
      Cox Proportional Hazard
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-1">
        Cox Proportional Hazard
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/coxph.html" title="Standard CoxPH  (API)" class="md-nav__link">
      Standard CoxPH  (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/nonlinear_coxph.html" title="DeepSurv/Nonlinear CoxPH (API)" class="md-nav__link">
      DeepSurv/Nonlinear CoxPH (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/semi_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-2" type="checkbox" id="nav-6-2">

    <label class="md-nav__link" for="nav-6-2">
      Multi-Task Logistic Regression (MTLR)
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-2">
        Multi-Task Logistic Regression (MTLR)
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_mtlr.html" title="Linear MTLR (API)" class="md-nav__link">
      Linear MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/neural_mtlr.html" title="Neural MTLR (API)" class="md-nav__link">
      Neural MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/mtlr_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-3" type="checkbox" id="nav-6-3">

    <label class="md-nav__link" for="nav-6-3">
      Non-Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-3">
        Non-Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/kaplan_meier.html" title="Kaplan Meier (API)" class="md-nav__link">
      Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/smooth_kaplan_meier.html" title="Smoothed Kaplan Meier (API)" class="md-nav__link">
      Smoothed Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/non_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-4" type="checkbox" id="nav-6-4">

    <label class="md-nav__link" for="nav-6-4">
      Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-4">
        Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/parametric.html" title="Parametric models (API)" class="md-nav__link">
      Parametric models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/parametric_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-5" type="checkbox" id="nav-6-5">

    <label class="md-nav__link" for="nav-6-5">
      Simulation
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-5">
        Simulation
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/simulations.html" title="Simulation models (API)" class="md-nav__link">
      Simulation models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/simulations_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-6" type="checkbox" id="nav-6-6">

    <label class="md-nav__link" for="nav-6-6">
      Survival Forest
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-6">
        Survival Forest
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/conditional_survival_forest.html" title="Conditional Survival Forest (API)" class="md-nav__link">
      Conditional Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/extra_survival_trees.html" title="Extra Survival Trees (API)" class="md-nav__link">
      Extra Survival Trees (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/random_survival_forest.html" title="Random Survival Forest (API)" class="md-nav__link">
      Random Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_forest.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-7" type="checkbox" id="nav-6-7">

    <label class="md-nav__link" for="nav-6-7">
      Survival SVM
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-7">
        Survival SVM
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_svm.html" title="Linear Survival SVM (API)" class="md-nav__link">
      Linear Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/kernel_svm.html" title="Kernel Survival SVM (API)" class="md-nav__link">
      Kernel Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_svm.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-7" type="checkbox" id="nav-7">

    <label class="md-nav__link" for="nav-7">
      Performance metrics
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-7">
        Performance metrics
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../metrics/c_index.html" title="C-index" class="md-nav__link">
      C-index
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../metrics/brier_score.html" title="Brier Score" class="md-nav__link">
      Brier Score
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-8" type="checkbox" id="nav-8">

    <label class="md-nav__link" for="nav-8">
      Miscellaneous
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-8">
        Miscellaneous
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../miscellaneous/activation_functions.html" title="Activation Functions" class="md-nav__link">
      Activation Functions
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../miscellaneous/save_load.html" title="Saving/Loading models" class="md-nav__link">
      Saving/Loading models
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../miscellaneous/tips.html" title="Tips for fitting models" class="md-nav__link">
      Tips for fitting models
    </a>
  </li>


      </ul>
    </nav>
  </li>


  </ul>
</nav>
                  </div>
                </div>
              </div>


              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">

<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#1-introduction" title="1 - Introduction" class="md-nav__link">
    1 - Introduction
  </a>

</li>

        <li class="md-nav__item">
  <a href="#2-set-up" title="2 - Set up" class="md-nav__link">
    2 - Set up
  </a>

</li>

        <li class="md-nav__item">
  <a href="#3-dataset" title="3 - Dataset" class="md-nav__link">
    3 - Dataset
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#31-overview" title="3.1 - Overview" class="md-nav__link">
    3.1 - Overview
  </a>

</li>

          <li class="md-nav__item">
  <a href="#32-from-categorical-to-numerical" title="3.2 - From categorical to numerical" class="md-nav__link">
    3.2 - From categorical to numerical
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#4-exploratory-data-analysis" title="4 - Exploratory Data Analysis" class="md-nav__link">
    4 - Exploratory Data Analysis
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#41-null-values-and-duplicates" title="4.1 - Null values and duplicates" class="md-nav__link">
    4.1 - Null values and duplicates
  </a>

</li>

          <li class="md-nav__item">
  <a href="#42-correlations" title="4.2 - Correlations" class="md-nav__link">
    4.2 - Correlations
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#5-modeling" title="5 - Modeling" class="md-nav__link">
    5 - Modeling
  </a>

</li>

        <li class="md-nav__item">
  <a href="#6-cross-validation" title="6 - Cross Validation" class="md-nav__link">
    6 - Cross Validation
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#61-c-index" title="6.1 - C-index" class="md-nav__link">
    6.1 - C-index
  </a>

</li>

          <li class="md-nav__item">
  <a href="#62-brier-score" title="6.2 - Brier Score" class="md-nav__link">
    6.2 - Brier Score
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#7-predictions" title="7 - Predictions" class="md-nav__link">
    7 - Predictions
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#71-overall-predictions" title="7.1 - Overall predictions" class="md-nav__link">
    7.1 - Overall predictions
  </a>

</li>

          <li class="md-nav__item">
  <a href="#72-individual-predictions" title="7.2 - Individual predictions" class="md-nav__link">
    7.2 - Individual predictions
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#8-conclusion" title="8 - Conclusion" class="md-nav__link">
    8 - Conclusion
  </a>

</li>

        <li class="md-nav__item">
  <a href="#references" title="References" class="md-nav__link">
    References
  </a>

</li>





    </ul>

</nav>
                  </div>
                </div>
              </div>


          <div class="md-content">
            <article class="md-content__inner md-typeset">


                  <a href="https://github.com/square/pysurvival/edit/master/docs/tutorials/credit_risk.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>


                <!--  Tutorial - Credit Risk -->

<style>
  h1, h2, h3, h4 { color: #04A9F4; }
</style>

<h1 id="computing-the-speed-of-repayment-of-loans">Computing the speed of repayment of loans</h1>
<h2 id="1-introduction">1 - Introduction</h2>
<p>Credit Risk refers to the likelihood that a borrower will not be able to repay a loan contracted by a lender. Thus throughout the years, financial institutions have developed various ways to quantify that risk so as to limit their exposure.</p>
<p>Here, instead of simply modeling whether a borrower will repay, by using Survival Analysis, it becomes possible to determine when this will happen. Indeed, it is easy to consider that fully repaying a loan is an <strong>explicit event</strong>, and therefore not having paid back the loan yet can be defined as the censored situation.</p>
<p>By using this configuration, banks, credit unions, or fintech startups in the lending space can predict the speed of repayment of a loan. This will help these institutions mitigate losses due to bad debt, customize interest rates, improve cash flow and credit collections, and determine which customers are likely to bring in the most revenue throughout a variety of products.</p>
<hr />
<h2 id="2-set-up">2 - Set up</h2>
<p>In this tutorial, we will be using the German Credit dataset, which was originally provided by Professor Dr. Hans Hofmann of the University of Hamburg and available on the <a href="https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data)">UCI Machine Learning Repository</a>. The current version was adapted to be directly usable with a minimum amount of feature transformation..</p>
<h2 id="3-dataset">3 - Dataset</h2>
<h3 id="31-overview">3.1 - Overview</h3>
<p>The dataset contains information useful to assess the borrowers creditworthiness as well as socio-demographic elements:</p>
<table>
<thead>
<tr>
<th>Feature category</th>
<th>Feature name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span style="color:blue"> Time </span></td>
<td><code>duration</code></td>
<td>numerical</td>
<td>Duration in month</td>
</tr>
<tr>
<td><span style="color:blue"> Event </span></td>
<td><code>full_repaid</code></td>
<td>categorical</td>
<td>Specifies if the loan was fully repaid</td>
</tr>
<tr>
<td>Socio-Demographic</td>
<td><code>age</code></td>
<td>numerical</td>
<td>Age of the borrower (in years)</td>
</tr>
<tr>
<td>Socio-Demographic</td>
<td><code>foreign_worker</code></td>
<td>numerical</td>
<td>Indicates if the borrower is a foreign worker</td>
</tr>
<tr>
<td>Socio-Demographic</td>
<td><code>personal_status</code></td>
<td>categorical</td>
<td>Gender and Marital status</td>
</tr>
<tr>
<td>Socio-Demographic</td>
<td><code>people_liable</code></td>
<td>numerical</td>
<td>Number of people being liable to provide maintenance for</td>
</tr>
<tr>
<td>Socio-Demographic</td>
<td><code>telephone</code></td>
<td>numerical</td>
<td>Indicates if the borrower owns a phone</td>
</tr>
<tr>
<td>Employment</td>
<td><code>employment_years</code></td>
<td>categorical</td>
<td>Years of employment at current job</td>
</tr>
<tr>
<td>Employment</td>
<td><code>job</code></td>
<td>categorical</td>
<td>Employment status</td>
</tr>
<tr>
<td>Residence</td>
<td><code>housing</code></td>
<td>categorical</td>
<td>Residential status of the borrower</td>
</tr>
<tr>
<td>Residence</td>
<td><code>present_residence</code></td>
<td>numerical</td>
<td>Years living at current residence</td>
</tr>
<tr>
<td>Loan information</td>
<td><code>amount</code></td>
<td>numerical</td>
<td>Amount of money borrowed</td>
</tr>
<tr>
<td>Loan information</td>
<td><code>installment_rate</code></td>
<td>numerical</td>
<td>Percentage of amount borrowed that will be charged by a lender to a borrower.</td>
</tr>
<tr>
<td>Loan information</td>
<td><code>purpose</code></td>
<td>categorical</td>
<td>Reason to get a loan</td>
</tr>
<tr>
<td>Bank information</td>
<td><code>checking_account_status</code></td>
<td>categorical</td>
<td>Status of the checking account</td>
</tr>
<tr>
<td>Bank information</td>
<td><code>credit_history</code></td>
<td>categorical</td>
<td>Credit history of the borrower</td>
</tr>
<tr>
<td>Bank information</td>
<td><code>number_of_credits</code></td>
<td>numerical</td>
<td>Number of existing credits at this bank</td>
</tr>
<tr>
<td>Bank information</td>
<td><code>other_installment_plans</code></td>
<td>categorical</td>
<td>Type of installments plans the borrower already has</td>
</tr>
<tr>
<td>Bank information</td>
<td><code>savings_account_status</code></td>
<td>categorical</td>
<td>Status of the saving account</td>
</tr>
<tr>
<td>Collateral/Guarantor</td>
<td><code>property</code></td>
<td>categorical</td>
<td>Type of valuable assets the borrower owns</td>
</tr>
<tr>
<td>Collateral/Guarantor</td>
<td><code>other_debtors</code></td>
<td>categorical</td>
<td>Indicate if someone else will be involved in the repayment or is guaranteeing the loan</td>
</tr>
</tbody>
</table>
<p><div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing modules</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> pyplot <span style="color: #008000; font-weight: bold">as</span> plt
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.datasets</span> <span style="color: #008000; font-weight: bold">import</span> Dataset
<span style="color: #666666">%</span>pylab inline

<span style="color: #408080; font-style: italic"># Reading the dataset</span>
raw_dataset <span style="color: #666666">=</span> Dataset(<span style="color: #BA2121">&#39;credit_risk&#39;</span>)<span style="color: #666666">.</span>load()
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The raw_dataset has the following shape: {}.&quot;</span><span style="color: #666666">.</span>format(raw_dataset<span style="color: #666666">.</span>shape))
raw_dataset<span style="color: #666666">.</span>head(<span style="color: #666666">3</span>)
</pre></div>
Here is an overview of the raw dataset:</p>
<table>
<thead>
<tr>
<th>checking_account_status</th>
<th>duration</th>
<th>credit_history</th>
<th>...</th>
<th>foreign_worker</th>
<th>full_repaid</th>
</tr>
</thead>
<tbody>
<tr>
<td>below_0</td>
<td>6</td>
<td>critical_account</td>
<td>...</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>0_to_200</td>
<td>48</td>
<td>existing_credit_paid</td>
<td>...</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>no_account</td>
<td>12</td>
<td>critical_account</td>
<td>...</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<h3 id="32-from-categorical-to-numerical">3.2 - From categorical to numerical</h3>
<p>There are several categorical features that need to encoded into one-hot vectors:</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># From category to numerical</span>
category_columns <span style="color: #666666">=</span> [
    <span style="color: #BA2121">&#39;checking_account_status&#39;</span>, <span style="color: #BA2121">&#39;credit_history&#39;</span>, <span style="color: #BA2121">&#39;purpose&#39;</span>,
    <span style="color: #BA2121">&#39;savings_account_status&#39;</span>, <span style="color: #BA2121">&#39;employment_years&#39;</span>, <span style="color: #BA2121">&#39;personal_status&#39;</span>,
    <span style="color: #BA2121">&#39;other_debtors&#39;</span>, <span style="color: #BA2121">&#39;property&#39;</span>, <span style="color: #BA2121">&#39;other_installment_plans&#39;</span>, <span style="color: #BA2121">&#39;housing&#39;</span>, <span style="color: #BA2121">&#39;job&#39;</span>]
dataset <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>get_dummies(raw_dataset, columns<span style="color: #666666">=</span>category_columns, drop_first<span style="color: #666666">=</span><span style="color: #008000">True</span>)

<span style="color: #408080; font-style: italic"># Creating the time and event columns</span>
time_column <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;duration&#39;</span>
event_column <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;full_repaid&#39;</span>

<span style="color: #408080; font-style: italic"># Creating the features</span>
features <span style="color: #666666">=</span> np<span style="color: #666666">.</span>setdiff1d(dataset<span style="color: #666666">.</span>columns, [time_column, event_column] )<span style="color: #666666">.</span>tolist()
</pre></div>

<hr />
<h2 id="4-exploratory-data-analysis">4 - Exploratory Data Analysis</h2>
<p>As this tutorial is mainly designed to provide an example of how to use PySurvival, we will not do a thorough exploratory data analysis here but greatly encourage the reader to do so by checking the <a href="maintenance.html#4-exploratory-data-analysis">predictive maintenance tutorial that provides a detailed analysis.</a></p>
<p>Here, we will just check if the dataset contains Null values or if it has duplicated rows. Then, we will take a look at feature correlations.</p>
<h3 id="41-null-values-and-duplicates">4.1 - Null values and duplicates</h3>
<p>The first thing to do is checking if the raw_dataset contains Null values and has duplicated rows.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Checking for null values</span>
N_null <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(dataset[features]<span style="color: #666666">.</span>isnull()<span style="color: #666666">.</span>sum())
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The raw_dataset contains {} null values&quot;</span><span style="color: #666666">.</span>format(N_null)) <span style="color: #408080; font-style: italic">#0 null values</span>

<span style="color: #408080; font-style: italic"># Removing duplicates if there exist</span>
N_dupli <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(dataset<span style="color: #666666">.</span>duplicated(keep<span style="color: #666666">=</span><span style="color: #BA2121">&#39;first&#39;</span>))
dataset <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>drop_duplicates(keep<span style="color: #666666">=</span><span style="color: #BA2121">&#39;first&#39;</span>)<span style="color: #666666">.</span>reset_index(drop<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The raw_dataset contains {} duplicates&quot;</span><span style="color: #666666">.</span>format(N_dupli))

<span style="color: #408080; font-style: italic"># Number of samples in the dataset</span>
N <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>]
</pre></div>
As it turns out the raw_dataset doesn't have any Null values or duplicates.</p>
<h3 id="42-correlations">4.2 - Correlations</h3>
<p>Let's compute and visualize the correlation between the features
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> correlation_matrix
correlation_matrix(dataset[features], figure_size<span style="color: #666666">=</span>(<span style="color: #666666">40</span>,<span style="color: #666666">15</span>), text_fontsize<span style="color: #666666">=7</span>)
</pre></div></p>
<p><center><img src="images/credit_correlations.png" alt="PySurvival - Credit Risk - Correlations" title="PySurvival - Credit Risk - Correlations" width=100%, height=100%  /></center>
<center>Figure 1 - Correlations </center></p>
<p>Based on the correlations chart, we should remove the following features</p>
<ul>
<li><code>credit_history_existing_credit_paid</code></li>
<li><code>housing_own</code></li>
</ul>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>to_remove <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;credit_history_existing_credit_paid&#39;</span>, <span style="color: #BA2121">&#39;housing_own&#39;</span>]
features <span style="color: #666666">=</span> np<span style="color: #666666">.</span>setdiff1d(features, to_remove)<span style="color: #666666">.</span>tolist()
</pre></div>

<hr />
<h2 id="5-modeling">5 - Modeling</h2>
<p>So as to perform cross-validation later on and assess the performances of the model, let's split the dataset into training and testing sets.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Building training and testing sets</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
index_train, index_test <span style="color: #666666">=</span> train_test_split( <span style="color: #008000">range</span>(N), test_size <span style="color: #666666">=</span> <span style="color: #666666">0.4</span>)
data_train <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>loc[index_train]<span style="color: #666666">.</span>reset_index( drop <span style="color: #666666">=</span> <span style="color: #008000">True</span> )
data_test  <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>loc[index_test]<span style="color: #666666">.</span>reset_index( drop <span style="color: #666666">=</span> <span style="color: #008000">True</span> )

<span style="color: #408080; font-style: italic"># Creating the X, T and E inputs</span>
X_train, X_test <span style="color: #666666">=</span> data_train[features], data_test[features]
T_train, T_test <span style="color: #666666">=</span> data_train[time_column], data_test[time_column]
E_train, E_test <span style="color: #666666">=</span> data_train[event_column], data_test[event_column]
</pre></div></p>
<p>Let's now fit a <a href="../models/neural_mtlr.html">Neural MTLR</a> model to the training set. </p>
<p><em>Note: The choice of the structure of the neural network was obtained using grid-search hyperparameters selection, not displayed in this tutorial.</em>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.models.multi_task</span> <span style="color: #008000; font-weight: bold">import</span> NeuralMultiTaskModel

<span style="color: #408080; font-style: italic"># Initializing the Neural MTLR with a time axis split into 100 intervals</span>
structure <span style="color: #666666">=</span> [
                {<span style="color: #BA2121">&#39;activation&#39;</span>: <span style="color: #BA2121">&#39;ReLU&#39;</span>, <span style="color: #BA2121">&#39;num_units&#39;</span>: <span style="color: #666666">60</span>},
                {<span style="color: #BA2121">&#39;activation&#39;</span>: <span style="color: #BA2121">&#39;Swish&#39;</span>, <span style="color: #BA2121">&#39;num_units&#39;</span>: <span style="color: #666666">60</span>},
            ]
neural_mtlr <span style="color: #666666">=</span> NeuralMultiTaskModel(bins<span style="color: #666666">=100</span>, structure<span style="color: #666666">=</span>structure)

<span style="color: #408080; font-style: italic"># Fitting the model</span>
neural_mtlr<span style="color: #666666">.</span>fit(X_train, T_train, E_train,
                init_method <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;orthogonal&#39;</span>, optimizer <span style="color: #666666">=</span><span style="color: #BA2121">&#39;rprop&#39;</span>, lr <span style="color: #666666">=</span> <span style="color: #666666">1e-4</span>,
                l2_reg <span style="color: #666666">=</span> <span style="color: #666666">1e-1</span>,  l2_smooth <span style="color: #666666">=</span> <span style="color: #666666">1e-1</span>,
                batch_normalization <span style="color: #666666">=</span> <span style="color: #008000">True</span>,  bn_and_dropout <span style="color: #666666">=</span> <span style="color: #008000">True</span>,
                dropout<span style="color: #666666">=0.5</span>,  num_epochs <span style="color: #666666">=</span> <span style="color: #666666">500</span>)
</pre></div></p>
<p>We can take a look at the values of N-MTLR loss function to ensure that the fitting isn't incomplete
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> display_loss_values
display_loss_values(neural_mtlr)
</pre></div>
<center><img src="images/credit_loss.png" alt="PySurvival - Credit Risk - Loss function values" title="PySurvival - Credit Risk - Loss function values" width=100%, height=100%  /></center>
<center>Figure 2 - Neural MTLR loss function values </center></p>
<hr />
<h2 id="6-cross-validation">6 - Cross Validation</h2>
<p>In order to assess the model performance, we previously split the original dataset into training and testing sets, so that we can now compute its performance metrics on the testing set:</p>
<h3 id="61-c-index">6.1 - <a href="../metrics/c_index.html">C-index</a></h3>
<p>The <a href="../metrics/c_index.html">C-index</a> represents the global assessment of the model discrimination power: <strong><em>this is the model’s ability to correctly provide a reliable ranking of the survival times based on the individual risk scores</em></strong>. In general, when the C-index is close to 1, the model has an almost perfect discriminatory power; but if it is close to 0.5, it has no ability to discriminate between low and high risk subjects.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.metrics</span> <span style="color: #008000; font-weight: bold">import</span> concordance_index
c_index <span style="color: #666666">=</span> concordance_index(neural_mtlr, X_test, T_test, E_test)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;C-index: {:.2f}&#39;</span><span style="color: #666666">.</span>format(c_index)) <span style="color: #408080; font-style: italic">#0.76</span>
</pre></div>

<hr />
<h3 id="62-brier-score">6.2 - <a href="../metrics/brier_score.html">Brier Score</a></h3>
<p>The <strong><em><a href="../metrics/brier_score.html">Brier score</a> measures the average discrepancies between the status and the estimated probabilities at a given time.</em></strong>
Thus, the lower the score (<em>usually below 0.25</em>), the better the predictive performance. To assess the overall error measure across multiple time points, the Integrated Brier Score (IBS) is usually computed as well.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> integrated_brier_score
integrated_brier_score(neural_mtlr, X_test, T_test, E_test, t_max<span style="color: #666666">=100</span>,
                       figure_size<span style="color: #666666">=</span>(<span style="color: #666666">20</span>, <span style="color: #666666">6.5</span>) ) <span style="color: #408080; font-style: italic">#0.08</span>
</pre></div>

<p><center><img src="images/credit_brier.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Brier score & Prediction error curve" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Brier score & Prediction error curve" width=100%, height=100%  /></center>
<center>Figure 3 - Neural MTLR loss function values </center></p>
<p>The IBS is equal to <script type="math/tex">0.08</script> on the entire model time axis. This indicates that the model will have good predictive abilities.</p>
<hr />
<h2 id="7-predictions">7 - Predictions</h2>
<h3 id="71-overall-predictions">7.1 - Overall predictions</h3>
<p>Now that we have built a model that seems to provide great performances, let's compare the following:</p>
<ul>
<li>the time series of the actual and predicted number of loans that were fully repaid, for each time t.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> compare_to_actual
results <span style="color: #666666">=</span> compare_to_actual(neural_mtlr, X_test, T_test, E_test,
                            is_at_risk <span style="color: #666666">=</span> <span style="color: #008000">False</span>,  figure_size<span style="color: #666666">=</span>(<span style="color: #666666">16</span>, <span style="color: #666666">6</span>),
                            metrics <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;rmse&#39;</span>, <span style="color: #BA2121">&#39;mean&#39;</span>, <span style="color: #BA2121">&#39;median&#39;</span>])
</pre></div></li>
</ul>
<p><center><img src="images/credit_global_pred_1.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were fully repaid" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were fully repaid" width=100%, height=100%  /></center>
<center>Figure 4 - Actual vs Predicted - Number of loans that were fully repaid</center></p>
<ul>
<li>the time series of the actual and predicted number of loans that were still active, for each time t.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>results <span style="color: #666666">=</span> compare_to_actual(neural_mtlr, X_test, T_test, E_test,
                            is_at_risk <span style="color: #666666">=</span> <span style="color: #008000">True</span>,  figure_size<span style="color: #666666">=</span>(<span style="color: #666666">16</span>, <span style="color: #666666">6</span>),
                            metrics <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;rmse&#39;</span>, <span style="color: #BA2121">&#39;mean&#39;</span>, <span style="color: #BA2121">&#39;median&#39;</span>])
</pre></div></li>
</ul>
<p><center><img src="images/credit_global_pred_2.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were still active" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were still active" width=100%, height=100%  /></center>
<center>Figure 5 - Actual vs Predicted - Number of loans that were still active</center></p>
<p>Both comparisons show that the model do a great job predicting the number of loans that were fully repaid (<em>average absolute error of 4.5 loan</em>) or that were still active (<em>average absolute error of 18.4 loans</em>) for all times t of the 70+ months time window.</p>
<hr />
<h3 id="72-individual-predictions">7.2 - Individual predictions</h3>
<p>Now that we know that we can provide reliable predictions for an entire cohort, let's compute the speed of repayment at the individual level. The speed of repayment is given by <script type="math/tex">\text{Speed}(t) = 1 - \text{Survival}(t)</script>
</p>
<p>First, we can construct the risk groups based on risk scores distribution. The helper function <code>create_risk_groups</code>, which can be found in <code>pysurvival.utils</code>, will help us do that:
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> create_risk_groups

risk_groups <span style="color: #666666">=</span> create_risk_groups(model<span style="color: #666666">=</span>neural_mtlr, X<span style="color: #666666">=</span>X_test,
    use_log <span style="color: #666666">=</span> <span style="color: #008000">True</span>, num_bins<span style="color: #666666">=30</span>, figure_size<span style="color: #666666">=</span>(<span style="color: #666666">20</span>, <span style="color: #666666">4</span>),
    low<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>:<span style="color: #666666">0</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">1.64</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;red&#39;</span>},
    medium<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>: <span style="color: #666666">1.64</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">1.8</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;green&#39;</span>},
    high<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>:<span style="color: #666666">1.8</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">3</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;blue&#39;</span>}
    )
</pre></div></p>
<p><center><img src="images/credit_risk.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Risk groups" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Risk groups" width=100%, height=100%  /></center>
<center>Figure 6 - Creating risk groups </center></p>
<p><em>Note: The current choice of the lower and upper bounds for each group is based on my intuition; so feel free to change the values so as to match your situation instead.</em></p>
<p>Here, it is possible to distinguish 3 main groups, <em>low</em>, <em>medium</em> and <em>high</em> risk groups. Because the C-index is high, the model will be able to  rank the survival times of a random unit of each group, such that <script type="math/tex"> t_{high}  \leq t_{medium} \leq t_{low}</script>.</p>
<p>Let's randomly select individual unit in each group and compare their speed of repayment functions. To demonstrate our point, we will purposely select units which experienced an event to visualize the actual time of event.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Initializing the figure</span>
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">5</span>))

<span style="color: #408080; font-style: italic"># Selecting a random individual that experienced an event from each group</span>
groups <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> i, (label, (color, indexes)) <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(risk_groups<span style="color: #666666">.</span>items()) :

    <span style="color: #408080; font-style: italic"># Selecting the individuals that belong to this group</span>
    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(indexes) <span style="color: #666666">==</span> <span style="color: #666666">0</span> :
        <span style="color: #008000; font-weight: bold">continue</span>
    X <span style="color: #666666">=</span> X_test<span style="color: #666666">.</span>values[indexes, :]
    T <span style="color: #666666">=</span> T_test<span style="color: #666666">.</span>values[indexes]
    E <span style="color: #666666">=</span> E_test<span style="color: #666666">.</span>values[indexes]

    <span style="color: #408080; font-style: italic"># Randomly extracting an individual that experienced an event</span>
    choices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argwhere((E<span style="color: #666666">==1.</span>))<span style="color: #666666">.</span>flatten()
    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(choices) <span style="color: #666666">==</span> <span style="color: #666666">0</span> :
        <span style="color: #008000; font-weight: bold">continue</span>
    k <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice( choices, <span style="color: #666666">1</span>)[<span style="color: #666666">0</span>]

    <span style="color: #408080; font-style: italic"># Saving the time of event</span>
    t <span style="color: #666666">=</span> T[k]

    <span style="color: #408080; font-style: italic"># Computing the CDF for all times t</span>
    cdf <span style="color: #666666">=</span> <span style="color: #666666">1.</span> <span style="color: #666666">-</span> neural_mtlr<span style="color: #666666">.</span>predict_survival(X[k, :])<span style="color: #666666">.</span>flatten()

    <span style="color: #408080; font-style: italic"># Displaying the functions</span>
    label_ <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;{} risk&#39;</span><span style="color: #666666">.</span>format(label)
    plt<span style="color: #666666">.</span>plot(neural_mtlr<span style="color: #666666">.</span>times, cdf, color <span style="color: #666666">=</span> color, label<span style="color: #666666">=</span>label_, lw<span style="color: #666666">=2</span>)
    groups<span style="color: #666666">.</span>append(label)

    <span style="color: #408080; font-style: italic"># Actual time</span>
    plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=</span>t, color<span style="color: #666666">=</span>color, ls <span style="color: #666666">=</span><span style="color: #BA2121">&#39;--&#39;</span>)
    ax<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">&#39;T={:.1f}&#39;</span><span style="color: #666666">.</span>format(t), xy<span style="color: #666666">=</span>(t, <span style="color: #666666">0.5*</span>(<span style="color: #666666">1.+0.2*</span>i)),
        xytext<span style="color: #666666">=</span>(t, <span style="color: #666666">0.5*</span>(<span style="color: #666666">1.+0.2*</span>i)), fontsize<span style="color: #666666">=12</span>)

<span style="color: #408080; font-style: italic"># Show everything</span>
groups_str <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;, &#39;</span><span style="color: #666666">.</span>join(groups)
title <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Comparing cumulative density functions between {} risk grades&quot;</span>
title <span style="color: #666666">=</span> title<span style="color: #666666">.</span>format(groups_str)
plt<span style="color: #666666">.</span>legend(fontsize<span style="color: #666666">=12</span>)
plt<span style="color: #666666">.</span>title(title, fontsize<span style="color: #666666">=15</span>)
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">0</span>, <span style="color: #666666">65</span>)
plt<span style="color: #666666">.</span>ylim(<span style="color: #666666">0</span>, <span style="color: #666666">1.05</span>)
plt<span style="color: #666666">.</span>show()
</pre></div></p>
<p><center><img src="images/credit_individual_speed.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Predicting individual speed of repayment functions" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Predicting individual speed of repayment functions" width=100%, height=100%  /></center>
<center>Figure 7 - Predicting individual speed of repayment functions </center></p>
<p>As we can see, the model manages to perfectly predict the event time, here it corresponds to a sudden increase in the individual speed of repayment function.</p>
<hr />
<h2 id="8-conclusion">8 - Conclusion</h2>
<p>We can now save our model so as to put it in production and score future borrowers.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Let&#39;s now save our model</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils</span> <span style="color: #008000; font-weight: bold">import</span> save_model
save_model(neural_mtlr, <span style="color: #BA2121">&#39;/Users/xxx/Desktop/credit_neural_mtlr.zip&#39;</span>)
</pre></div></p>
<p>Thanks to Survival Analysis, we can see that it is indeed possible to predict the speed of repayment of loans and forecast the number of loans that will be fully repaid throughout time, which is a great advantage over classification modeling.</p>
<hr />
<h2 id="references">References</h2>
<ul>
<li><a href="https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data)">German Credit dataset - UCI Machine Learning Repository</a></li>
</ul>









            </article>
          </div>
        </div>
      </main>


<footer class="md-footer">

  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">

          <div class="md-footer-copyright__highlight">
            Copyright &copy; 2019 Square Inc.
          </div>



        brought to you by
        <a href="https://squareup.com/us">
        <img src="images/Square_logo.png" alt="Square Logo" title="Square Logo" width=20%, height=20% align="center"  /></a>


      </div>

  <div class="md-footer-social">
    <link rel="stylesheet" href="../assets/fonts/font-awesome.css">

      <a href="https://pysurvival.io/" class="md-footer-social__link fa fa-home"></a>

      <a href="https://github.com/square/pysurvival" class="md-footer-social__link fa fa-github"></a>

  </div>

    </div>
  </div>
</footer>

    </div>

      <script src="../assets/javascripts/application.d9aa80ab.js"></script>

      <script>app.initialize({version:"1.0.4",url:{base:".."}})</script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


  </body>
</html>
